<?php	
	include 'init.php';		
	
	$rfilename	= $_GET['parm'];
	
	//if (!$rfilename)
	//{
	//	$rfilename = "/var/spool/sms/incoming/mob.txt";
	//}

	$rfile = fopen($rfilename, "r") or exit("Unable to open file!");
	
	$wfilename = tempnam("","");
	$wfile=fopen($wfilename,"w");
	
	while(!feof($rfile))
	{
		$line = fgets($rfile);
		if (substr($line,0,5) == "From:")
		{
			$cellphone = substr($line,6);
		}
		if ($readtherest)
		{
			$message = $message . " " . $line;
		}
		if (substr($line,0,7) == "Length:")
		{	
			$readtherest = true;
		}
	}
	
	$cellphone 	= substr($cellphone,2,8);
	$message 	= ltrim($message);
	$message 	= str_replace("\n", "", $message);
	
	mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); 
	@mysql_select_db("$DBName") or die("Unable to select database $DBName"); 
	
	$sqlquery 	= "select * from $TSendSMS where CellPhone = '$cellphone' order by SMSId desc Limit 0, 1"; 
	$results 	= mysql_query($sqlquery) or die("Unable to query : " . mysql_error());
	$line 		= mysql_fetch_array($results, MYSQL_ASSOC);
	
	$listid_ins = $line["ListId"];
	
	mysql_free_result($results);
	
	mysql_query("INSERT INTO $TSendSMS (ListId, CellPhone, Message, status) VALUES ('$listid_ins','$cellphone','$message','Received')");	
	mysql_close();
	
	//print "$listid_ins - " . "$message";
?>